package avicit.bdp.dms.dss.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.common.utils.DateUtils;
import avicit.bdp.dms.dss.dao.DssLogDAO;
import avicit.bdp.dms.dss.dto.DssLogDTO;
import avicit.platform6.api.system.ConvertColumnClient;
import avicit.platform6.api.system.impl.SystemConstant;
import avicit.platform6.commons.utils.BusinessUtil;
import avicit.platform6.core.exception.BusinessException;
import avicit.platform6.core.exception.BusinessException;
import avicit.platform6.core.rest.msg.QueryRespBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 数据服务_日志服务
 *
 * @author yangpf
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DssLogService extends BaseService<DssLogDAO, DssLogDTO> {
    @Autowired
    private ConvertColumnClient convertColumnClient;

    /**
     * 分页查询资源日志列表
     *
     * @param resourceId   数据服务ID
     * @param startDate    开始时间
     * @param endDate      结束时间
     * @param callUserName 调用人姓名
     * @param type         类型
     * @return 日志列表
     */
    public QueryRespBean<DssLogDTO> resourceLogs(String resourceId, String startDate,
                                                 String endDate, String callUserName,
                                                 Integer type, Integer pageNo,
                                                 Integer pageSize) {

        QueryRespBean<DssLogDTO> queryRespBean = new QueryRespBean<>();
        try {
            if (StringUtils.isBlank(resourceId)) {
                throw new BusinessException("请检查参数！");
            }
            Date start = null;
            Date end = null;
            if (StringUtils.isNotEmpty(startDate)) {
                start = DateUtils.getScheduleDate(startDate + " 00:00:00");
            }
            if (StringUtils.isNotEmpty(endDate)) {
                end = DateUtils.getScheduleDate(endDate + " 23:59:59");
            }
            PageHelper.startPage(pageNo, pageSize);
            Page<DssLogDTO> dataList = this.mapper.resourceLogs(resourceId, start, end, callUserName, type);
            //valueConvert(dataList);
            queryRespBean.setResult(dataList);
            BdpLogUtil.log4Query(queryRespBean);
            return queryRespBean;
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }

    private void valueConvert(Page<DssLogDTO> dataList) {
        //循环组装请求数据
        Map<String, Set<String>> convertFormData = new HashMap<>();
        for (DssLogDTO dto : dataList) {
            BusinessUtil.createConvertSet(convertFormData, SystemConstant.USER, dto.getCallUserId());
        }
        if (convertFormData.size() > 0) {
            //获取请求结果
            Map<String, Map<String, String>> convertResultData = convertColumnClient.replace(convertFormData);
            //循环设置Alias或Name的值
            for (DssLogDTO dto : dataList) {
                dto.setCallUserName(BusinessUtil.convertFormat(convertResultData, SystemConstant.USER,
                        dto.getCallUserId()));
            }
        }
    }

}
